$(function () {
    $("#jqGrid").jqGrid({
        url: '../scheduleJob/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', width: 50, key: true},
            {label: '任务名称', name: 'jobName', index: 'job_name', width: 80},
            {
                label: '状态', name: 'status', width: 80, formatter: function (value, options, row) {
                    return T.showDictItemName('sys_status', value);
                }
            },
            {label: 'cron表达式', name: 'cronExpression', index: 'cron_expression', width: 80},
            {label: '描述', name: 'remark', index: 'remark', width: 80},
            {label: 'createTime', name: 'createTime', index: 'create_time', width: 80},
            {label: 'updateTime', name: 'updateTime', index: 'update_time', width: 80},
            {
                label: '操作',
                name: 'actions',
                sortable: false,
                width: 80,
                formatter: function (value, options, row, act) {
                    var opts = [];
                    opts.push('<a href="javascript:" class="btnList" title="编辑作业项" onclick="vm.updateRow(' + row.id + ')" ><i class="fa fa-pencil"></i></a>&nbsp;');
                    opts.push('<a href="javascript:" class="btnList" title="作业项明细" onclick="vm.dtlMgr(' + row.id + ')"><i class="fa fa-navicon"></i></a>&nbsp;');
                    if (row.status == 1) {
                        opts.push('<a href="javascript:" class="btnList" title="停用作业项" data-confirm="确认要停用该作业项吗？" data-click="vm.changeStatus(' + row.id + ',0)"><i class="glyphicon glyphicon-ban-circle"></i></a>&nbsp;');
                    }
                    if (row.status == 0) {
                        opts.push('<a href="javascript:" class="btnList" title="启用作业项" data-confirm="确认要启用该作业项吗？" data-click="vm.changeStatus(' + row.id + ',1)"><i class="glyphicon glyphicon-ok-circle"></i></a>&nbsp;');
                    }

                    return opts.join('');
                }
            }
        ],
        viewrecords: true,
        height: 800,
        rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.data",
            page: "page.pageNo",
            total: "page.pageCount",
            records: "page.count"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }, ondblClickRow: function (id) {
            location.href = "/skd/scheduleJobDtlTree.html?jobId=" + id;
        },
        loadComplete: function () {
            btnMoreEventBind();
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        isShowCond: false,
        btnText: '查询',
        qryForm: {},
        scheduleJob: {}
    },
    methods: {
        updateRow: function (id) {
            if (id == null) {
                return;
            }
            location.href = "scheduleJob_add.html?id=" + id;
        },
        changeStatus: function (id, status) {
            var url = "../scheduleJob/update";
            vm.scheduleJob.id = id;
            vm.scheduleJob.status = status;
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(vm.scheduleJob),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        reset: function () {

        },
        query: function () {
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {},
                page: 1
            }).trigger("reloadGrid");
        },
        toggle: function () {
            this.isShowCond = !this.isShowCond;

            if (this.isShowCond == true) {
                this.btnText = "隐藏"
            } else if (this.isShowCond == false) {
                this.btnText = "查询"
            }

        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }

            location.href = "scheduleJob_add.html?id=" + id;
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "/scheduleJob/delete",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        pause: function (event) {
            var jobIds = getSelectedRows();
            if (jobIds == null) {
                return;
            }

            confirm('确定要暂停选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "/scheduleJob/pause",
                    data: JSON.stringify(jobIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        resume: function (event) {
            var jobIds = getSelectedRows();
            if (jobIds == null) {
                return;
            }

            confirm('确定要恢复选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "/scheduleJob/resume",
                    data: JSON.stringify(jobIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        runOnce: function (event) {
            var jobIds = getSelectedRows();
            if (jobIds == null) {
                return;
            }

            confirm('确定要立即执行选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "/scheduleJob/run",
                    data: JSON.stringify(jobIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        dtlMgr: function (id) {
            if (id == null) {
                return;
            }
            location.href = "/skd/scheduleJobDtlTree.html?jobId=" + id;
        },
        execInst: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }

            location.href = "scheduleInstance.html?referType=scheduleJob&referId=" + id;
        }
    }
});
